package cn.smbms.service.provider;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.smbms.dao.bill.BillDao;
import cn.smbms.dao.bill.BillDaoImpl;
import cn.smbms.dao.provider.ProviderDao;
import cn.smbms.dao.provider.ProviderDaoImpl;
import cn.smbms.pojo.Provider;
import cn.smbms.tools.MyBatisUtil;

@Transactional
@Service("providerService")
public class ProviderServiceImpl implements ProviderService {
	
	@Autowired
	@Qualifier("providerDao")
	private ProviderDao providerDao;
	public ProviderDao getProviderDao() {
		return providerDao;
	}
	public void setProviderDao(ProviderDao providerDao) {
		this.providerDao = providerDao;
	}
	public ProviderServiceImpl(){
		providerDao=new ProviderDaoImpl();
	}
	
	@Transactional(propagation=Propagation.SUPPORTS)
	public boolean add(Provider provider) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			if(providerDao.add(provider) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Transactional(propagation=Propagation.SUPPORTS)
	public List<Provider> getProviderList(String proName,String proCode) {
		// TODO Auto-generated method stub
		try {
			return providerDao.getProviderList(proName, proCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 业务：根据ID删除供应商表的数据之前，需要先去订单表里进行查询操作
	 * 若订单表中无该供应商的订单数据，则可以删除
	 * 若有该供应商的订单数据，则不可以删除
	 * 返回值billCount
	 * 1> billCount == 0  删除---1 成功 （0） 2 不成功 （-1）
	 * 2> billCount > 0    不能删除 查询成功（0）查询不成功（-1）
	 * 
	 * ---判断
	 * 如果billCount = -1 失败
	 * 若billCount >= 0 成功
	 */
	@Transactional(propagation=Propagation.SUPPORTS)
	public int deleteProviderById(Integer delId) {
		// TODO Auto-generated method stub
		int billCount = -1;
		BillDao dao=new BillDaoImpl();
		try {
			billCount = dao.getBillCountByProviderId(delId);
			if(billCount == 0){
				providerDao.deleteProviderById(delId);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			billCount = -1;
		}
		return billCount;
	}

	@Transactional(propagation=Propagation.SUPPORTS)
	public Provider getProviderById(Integer id) {
		// TODO Auto-generated method stub
		Provider provider = null;
		try{
			provider = providerDao.getProviderById(id);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			provider = null;
		}
		return provider;
	}

	@Transactional(propagation=Propagation.SUPPORTS)
	public boolean modify(Provider provider) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			if(providerDao.modify(provider) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

}
